package com.company.OfferGuide;

import com.company.ListNode;

public class 合并两个排序的链表 {


    public static void main(String[] args) {

    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null) return null;
        ListNode head = null,p = null;
        while (l1 != null && l2 != null) {
            ListNode node = null;
            if (l1.val < l2.val) {
                node = l1;
                l1 = l1.next;
            } else {
                node = l2;
                l2 = l2.next;
            }
            if (head == null) {
                head = node;
                p = head;
            } else {
                p.next = node;
                p = node;
            }
        }
        if (l1 != null) {
            if (head == null) {
                head = l1;
            } else {
                p.next = l1;
            }
        }
        if (l2 != null) {
            if (head == null) {
                head = l2;
            } else {
                p.next = l2;
            }
        }
        return head;
    }

}
